﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Applications.Core.Client;
using System.Data;
using CdcSoftware.Pivotal.Applications.Core.Common;
using CdcSoftware.Pivotal.Engine;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using Pcm.Retail.Client;
using System.Windows.Forms;
using CdcSoftware.Pivotal.Engine.UI.Forms;
using CdcSoftware.Pivotal.Engine.UI.DataTypes.Navigation;

namespace Pcm.Retail.Client.FormTasks
{
    public partial class RE_Cashier_Log : FormClientTask
    {
        /// <summary>
        /// On load of the form.
        /// </summary>
        /// <returns>Returns true for successful save else false</returns> 
        /// <history>
        /// Revision#      Date            Author          Description
        /// 6.0.8.1        2011-09-06      DS              Initial version for Retail.                                                
        /// </history>
        public override void OnFormInitialized()
        {
            try
            {
                base.OnFormInitialized();

                //bool mobileuser = false;
                //mobileuser = this.SystemClient.SystemInformation.IsMobile;
                //if (mobileuser)
                //    PivotalMessageBox.Show("This is a mobile user", MessageBoxButtons.OK, MessageBoxIcon.Information);

                ((PivotalButton)this.FormControl.GetControlByName("PivBtnClockIn")).Enabled = false;
                ((PivotalButton)this.FormControl.GetControlByName("PivBtnClockOut")).Enabled = false;

                ButtonBarButton btnCancel = this.ButtonBar.Buttons["Cancel"];
                this.ButtonBar.Buttons.Clear();
                this.ButtonBar.Buttons.Add(btnCancel);                

                //Default the batch on the form.
                //RA:16/Aug/2012:Change for Mobile System:Start:
                DataTable batch = Globals.GetDataTable("RE_In Progress Batch for Store for today?", new object[] { this.PrimaryDataRow["Store_Id"] }, "RE_Batch_Id");
                //RA:16/Aug/2012:Change for Mobile System:End:
                //DataTable batch = Globals.GetDataTable("RE_In Progress Batch for Store?", new object[] { this.PrimaryDataRow["Store_Id"] }, "RE_Batch_Id");
                if (batch != null && batch.Rows.Count == 1)
                    this.PrimaryDataRow["Login_Batch_Id"] = batch.Rows[0]["RE_Batch_Id"];

                //Refresh the bottom status strip text.
                Globals.SetCashierStatusStrip(Globals.StatusStripPane.EnvironmentPane);

                //Event to always make the form non-dirty as we do not want to show the user prompt to save when existing the form.
                this.FormView.OnDirtyStateChanged += new EventHandler<Microsoft.Practices.CompositeUI.Utility.DataEventArgs<bool>>(FormView_OnDirtyStateChanged);
                 
                //Set the focus on Staff Code field.
                this.FormControl.GetControlByDataName("Login_Staff_Code").Focus();
            }
            catch (Exception ex)
            {
                Globals.HandleException(ex, true);
            }
        }

        /// <summary>
        /// Event to make the form non-dirty as we do not want to show the user prompt to save when existing the form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormView_OnDirtyStateChanged(object sender, Microsoft.Practices.CompositeUI.Utility.DataEventArgs<bool> e)
        {
            this.FormView.Dirty = false;
        }      
    }
}
